package com.cn.ccp.action.classschedule;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.TermSettingBean;
/**
 * 学期设置action
 * @author renyong
 *
 */
public class TermSettingAction extends BaseAction{
	private Logger log = Logger.getLogger(TermSettingAction.class); // log4j
	private TermSettingBean termSettingBean ;
	private List<TermSettingBean> termSettingList ;
	private SelectItem[] yearSelectItem ;// 年份下拉框值
	
	public TermSettingAction(){
		getpagedetail();
		yearSelectItem = new SelectItem[20];
		Calendar cur = Calendar.getInstance();
		int curYear = cur.get(Calendar.YEAR);
		curYear = curYear-1;
		for(int i=0; i<20;i++)
			yearSelectItem[i] = new SelectItem(String.valueOf(curYear+i),String.valueOf(curYear+i));
	}
	
	public String edit(){
		
		if(termSettingBean.getTermYear().equals("")){
			showMsg("termYear", "javax.faces.validator.RequiredValidator.FAILED");
			return "";
		}
		if(termSettingBean.getTermType() == null){
			showMsg("termType", "javax.faces.validator.RequiredValidator.FAILED");
			return "";
		}
		if(termSettingBean.getFirstWeekStart().equals("")){
			showMsg("firstWeekStart", "javax.faces.validator.RequiredValidator.FAILED");
			return "";
		}
		
		if(termSettingBean.getId() == 0){
			boolean flag = getTermSettingService().checkIsAllready(termSettingBean);
			if(flag){
				showMsg("termYear", "class_schedule_setting_page_term_year_already");
				return "";
			}
			return save();
		}
		return update();
	}
	
	public String save(){
		boolean flag = getTermSettingService().save(termSettingBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag = getTermSettingService().update(termSettingBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public void delete(){
		FacesContext context = FacesContext.getCurrentInstance();
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		getTermSettingService().delete(Integer.parseInt(id));
		getpagedetail();
	}
	
	public void showBean(){
		FacesContext context = FacesContext.getCurrentInstance();
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		if(id !=null)
			termSettingBean = (TermSettingBean) getTermSettingService().findTermSettingById(Integer.parseInt(id));
	}
	
	@SuppressWarnings("unchecked")
	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		if(id != null)
			termSettingBean = (TermSettingBean) getTermSettingService().findTermSettingById(Integer.parseInt(id));
		return UPDATE;
	}
	
	
	
	@Override
	public void getpagedetail() {
		setItemCount(getTermSettingService().getCountTermSetting());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		termSettingList = getTermSettingService().pageQueryTermSetting(start, pagesize);
	}

	public TermSettingBean getTermSettingBean() {
		if(termSettingBean == null)
			termSettingBean = new TermSettingBean();
		return termSettingBean;
	}

	public void setTermSettingBean(TermSettingBean termSettingBean) {
		this.termSettingBean = termSettingBean;
	}

	public List<TermSettingBean> getTermSettingList() {
		if(termSettingList == null)
			termSettingList = new ArrayList<TermSettingBean>();
		return termSettingList;
	}

	public void setTermSettingList(List<TermSettingBean> termSettingList) {
		this.termSettingList = termSettingList;
	}

	public SelectItem[] getYearSelectItem() {
		return yearSelectItem;
	}

	public void setYearSelectItem(SelectItem[] yearSelectItem) {
		this.yearSelectItem = yearSelectItem;
	}

	
}
